package com.stageadminserver.adminserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageadminserver.adminserver.mapper.AuthorMapper;
import com.stageadminserver.adminserver.mapper.CategoryMapper;
import com.stageadminserver.adminserver.mapper.UserMapper;
import com.stageadminserver.adminserver.mapper.WorksInfoMapper;
import com.stageadminserver.adminserver.service.WorksInfoService;
import com.stagecommon.common.pojo.Author;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.pojo.User;
import com.stagecommon.common.pojo.WorksInfo;
import com.stageuploadserver.uploadserver.pojo.UploadResponse;
import com.stageuploadserver.uploadserver.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author: yxl
 * @since: 2021-11-26
 **/
@Service
@Slf4j
public class WorksInfoServiceImpl extends ServiceImpl<WorksInfoMapper, WorksInfo> implements WorksInfoService {

    @Autowired
    private WorksInfoMapper worksInfoMapper;

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    MinioUtil minioUtil;

    @Override
    public RespBean<WorksInfo> selectWorkInfoById(Long workId) {
        return RespBean.operationSuccess("获取成功", 200, worksInfoMapper.selectWorkByWorkId(workId));
    }

    @Override
    public RespBean<IPage<WorksInfo>> getWorksInfoByPage(Long currentPage, Long size, WorksInfo worksInfo) {
        Page<WorksInfo> worksInfoPage = new Page<>(currentPage, size);
        return RespBean.operationSuccess("查询成功", 200, worksInfoMapper.selectWorksInfoByKeyword(worksInfoPage, worksInfo.getWorkName()));
    }

    @Override
    public RespBean<Integer> updateWorkById(WorksInfo worksInfo) {
        if (worksInfoMapper.updateById(worksInfo) == 1) {
            return RespBean.operationSuccess("更改成功！", 200, 1);
        }
        return RespBean.operationFail("更改失败！");
    }

    @Override
    public RespBean<Integer> deleteWorkById(WorksInfo worksInfo) {
        if (worksInfoMapper.deleteById(worksInfo) == 1) {
            if (!Objects.equals(null, worksInfo.getUsername())){
                authorMapper.decretementWorkNum(worksInfo.getUsername());
            }
            return RespBean.operationSuccess("删除作品成功！", 200, 1);
        }
        return RespBean.operationFail("删除作品失败！");
    }

    @Override
    public RespBean uploadFile(MultipartFile file, MultipartFile poster, WorksInfo worksInfo) throws Exception {
        String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        String fileName = "img/worksFile/" + originalFilename;

        String originalPoster = poster.getOriginalFilename();
        assert originalPoster != null;
        String posterName = "img/poster/" + originalPoster;

        UploadResponse bucket01 = minioUtil.uploadFile(file, "stage", fileName);
        UploadResponse bucket02 = minioUtil.uploadFile(poster, "stage", posterName);

        worksInfo.setRootCategoryId(categoryMapper.selectRootCategory(worksInfo.getCategoryId()));
        worksInfo.setPoster(bucket02.getMinIoUrl());
        worksInfo.setWorkFilename(bucket01.getMinIoUrl());
        worksInfo.setWorkDate(LocalDateTime.now());

        Author author = new Author();

        if (authorMapper.selectAuthorByUsername(worksInfo.getUsername()) == null){
            User user = userMapper.selectUserInformation(worksInfo.getUsername());
            author.setUserId(user.getUserId());
            author.setAuthorImg(user.getUserImg());
            author.setIntroduction(user.getIntroduction());
            author.setUsername(user.getUsername());
            author.setDemandNum(user.getDemandNum());
            authorMapper.insert(author);
        }
        authorMapper.incretementWork(worksInfo.getUsername());

        return worksInfoMapper.insert(worksInfo) == 1 ? RespBean.operationSuccess("上传成功", 200, worksInfo) : RespBean.operationSuccess("上传失败", -100, "error");
    }

    @Override
    public RespBean<List<WorksInfo>> selectAllWorks() {
        return RespBean.operationSuccess("查询成功", 200, worksInfoMapper.selectAllWorks());
    }




}
